package com.example.weiji_1;

import java.util.ArrayList;

import wordroid.model.TextList;
import wordroid.model.Texts;
import wordroid.model.UnitList;

import com.example.weiji_1.datebase.DataAccess;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Text1 extends Activity {

	private TextView view,view1,view2;
	private EditText edittext;
	private Button button1,button2,button3,button4;
	String[] text_1;
	String[] answer;
	int k = 1;
	private ArrayList<Texts> texts;
	private ArrayList<UnitList> text1;
	private ArrayList<TextList> textlist;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.text1);
		
		view = (TextView)super.findViewById(R.id.textView1);
		view1 = (TextView)super.findViewById(R.id.textView3);
		view2 = (TextView)super.findViewById(R.id.t4);
		edittext = (EditText)super.findViewById(R.id.edittext);
		button1 = (Button)super.findViewById(R.id.btn1);
		button2 = (Button)super.findViewById(R.id.btn2);
		button3 = (Button)super.findViewById(R.id.btn3);
		button4 = (Button)super.findViewById(R.id.btn4);
		
		DataAccess data = new DataAccess(this);
		texts = data.Text(null, null);
		//textlist = data.QueryText(null, null);
		text_1 = new String[texts.size()];
		answer = new String[texts.size()];
		
		int i=0;
		for (;i<texts.size();i++){
			text_1[i]=texts.get(i).getquestion();
			answer[i]=texts.get(i).getanswer();
		}
        view.setText(text_1[0]);
        button1.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String ss = edittext.getText().toString();
				Log.i("text1","ss="+ss);
				SQLdm s = new SQLdm();  
		        final SQLiteDatabase db =s.openDatabase(getApplicationContext());   
		        Cursor cursor = db.rawQuery("select * from test1 where identifier=?", new String[]{"1"+""});   
		        String name1 = null;  
		        if(cursor.moveToFirst()){    
		            name1 = cursor.getString(cursor.getColumnIndex("answer"));  
		        }
		        Log.i("text1","name1="+name1);
		        if(name1.equals(ss))
		        	view2.setText("绛旀姝ｇ‘");
		        if("".equals(edittext.getText().toString().trim()))
		        	Toast.makeText(getApplicationContext(), "璇疯緭鍏ヤ綘鐨勭瓟妗�", 0).show();
		        else
		        	view2.setText("绛旀閿欒"+answer[k]);
		        
		        view2.setVisibility(View.VISIBLE);
		        	
			}
			});
        
        button2.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				k--;
				view.setText(text_1[k]);
				view2.setEnabled(false);
				 view2.setVisibility(View.GONE);
				
				
		        	
			}
			});
        
        button3.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "宸叉敹钘�", 0).show();
				
			}
			});
        button4.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				view.setText(text_1[k]);
				k++;
				view2.setVisibility(View.GONE);
				
			}
			});
		
}
}
